package com.hqyj.manager.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.manager.service.OrderService;
import com.hqyj.manager.service.ProductService;
import com.hqyj.pojo.Order;
import com.hqyj.pojo.OrderItem;
import com.hqyj.pojo.Product;
import com.hqyj.pojo.User;
import com.hqyj.tmall.service.OrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;
    @Autowired
    private OrderItemService orderItemService;

    @RequestMapping("/list")
    public String orderList(ModelMap modelMap,
                            @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
                            @RequestParam(value = "inputName", defaultValue = " ") String inputName){

        PageInfo<Order> pageInfo = orderService.findAllOrder(currentPage, pageSize,inputName);
        System.out.println(pageInfo.getList());
        modelMap.addAttribute("orderList",pageInfo.getList());
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("inputName",inputName);//查询回显
        modelMap.addAttribute("totalNum",pageInfo.getTotal());//查询回显
        return "/orderTmall/order_list";
    }

    @RequestMapping("/deleteOrderById")
    @ResponseBody
    public String deleteOrderById(Integer orderId){
        Integer integer = orderService.deleteOrderById(orderId);
        return integer+"";
    }

    @RequestMapping("/deleteSomeByIds")
    @ResponseBody
    public String deleteSomeByIds(String oIds){
        Integer integer = orderService.deleteSomeByIds(oIds);
        if(integer>0){
            return "1";
        }
        return "0";
    }

    @RequestMapping("/updateOrder")
    public String updateOrder(ModelMap modelMap,Integer oId){
        List<Order> orderList = orderService.queryOrderById(oId);
        modelMap.addAttribute("orderList",orderList);
        return "/orderTmall/order_update";
    }

    @RequestMapping("/updateOrderById")
    @ResponseBody
    public String updateOrderById(ModelMap modelMap, Order order){
        System.out.println(order);
        Integer integer = orderService.updateOrderById(order);
        return integer+"";
    }

    @RequestMapping("/orderDetail")
    public String orderDetail(ModelMap modelMap,Integer oId,
                              @RequestParam(value = "inputName", defaultValue = " ") String inputName){
        List<Order> orderList = orderService.queryOrderById(oId);
        List<Product> productList = productService.queryOrderProduct(oId,inputName);

        modelMap.addAttribute("productList",productList);
        modelMap.addAttribute("orderList",orderList);
        modelMap.addAttribute("postAge","免邮");

        return "/orderTmall/order_detail";
    }

    @RequestMapping("/deleteOrderItemById")
    @ResponseBody
    public String deleteOrderItemById(Integer productId){
        System.out.println(productId);
       Integer integer = orderService.deleteOrderItemById(productId);
       if(integer>0){
           return "1";
       }
        return "0";
    }


}
